#!/bin/bash
#
# This script replaces Carthage checkouts with symlinks pointed inside the
# parent directory (..), useful for developing dependencies in coordination with
# the parent project, while avoiding the need for submodules.
#
# Usage:
#   cd ProjectFolder && /path/to/carthage-developer-checkouts

mkdir -p "Carthage/Checkouts/"

project_dir=$PWD
parent_dir=$(dirname "$PWD")

sed -E 's/(github|git|binary) \"([^\"]+)\" \"([^\"]+)\"/\2 \3/g' Cartfile.resolved | while read line
do
    read -a array <<< "$line"

    # Handles:
    # - ReactiveCocoa/ReactiveSwift > ReactiveSwift
    # - https://github.com/Carthage/Carthage.git > Carthage
    # - https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json > Mapbox-iOS-SDK
    dependency=`basename ${array[0]} | awk -F'.' '{ print $1 }'`

    version=${array[1]}

    echo -e "Cartfile.resolved[$dependency] at $version"

    dependency_dir="$parent_dir/$dependency"
    checkout_dir="Carthage/Checkouts/$dependency"

    echo "*** Setting up $dependency"

    if [ -d "$dependency_dir/.git" ]
    then
        pushd "$dependency_dir" >/dev/null

        git rev-parse "$version^{commit}" >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
            echo -e "\tFetching"
            git fetch --prune --quiet || exit $?
        fi

        echo -e "\tChecking out $version"
        git checkout --quiet "$version" || exit $?

        # TODO: Update submodules

        popd >/dev/null

        echo -e "\tSymlinking $dependency/Carthage/Build -> $project_dir/Carthage/Build"
        mkdir -p "$dependency_dir/Carthage" || exit $?
        rm -rf "$dependency_dir/Carthage/Build" || exit $?
        ln -s "$project_dir/Carthage/Build" "$dependency_dir/Carthage/Build" || exit $?

        echo -e "\tSymlinking $checkout_dir -> $dependency_dir"
        rm -rf "$checkout_dir" || exit $?
        ln -s "$dependency_dir" "$checkout_dir" || exit $?
    else
        echo -e "\tWarning: $dependency has not yet been cloned to $dependency_dir, skipping symlink."
    fi

    echo
done
